package com.gxnzd.communityelderly.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxnzd.communityelderly.commom.R;
import com.gxnzd.communityelderly.entity.Employee;
import com.gxnzd.communityelderly.service.EmployeeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * ClassName: UserController
 * Package: com.gxnzd.communityelderly.controller
 * Description:
 *
 * @Author 胡海峰
 * @Create 2024/10/1 21:23
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;


    @PostMapping("/login")
    public R login(@RequestBody Employee employee){
       return employeeService.login_user(employee);
    }

    @GetMapping("/logout")
    public R logout(HttpServletRequest request){

        return employeeService.logoutToken(request);
    }

    /**
     * 添加
     * @param employee
     * @return
     */
    @PostMapping("/addEmployee")
    public R addEmployee(@RequestBody Employee employee){
        return employeeService.insertEmployee(employee);
    }


    /**
     * 分页获取
     * @param page
     * @param pageSize
     * @param userName
     * @param name
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<Page> list(Integer page, Integer pageSize,String userName,String name ,Integer type){
        return employeeService.getList(page,pageSize,userName,name,type);
    }

    /**
     * 修改用户
     * @param employee
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Employee employee){
        return employeeService.updateEmployee(employee);
    }

    /**
     * 通过id删除员工
     * @param empId
     * @return
     */
    @DeleteMapping("/delete/{empId}")
    public R delete(@PathVariable Long empId){
        return employeeService.deleteEmployee(empId);
    }

    /**
     * 查询员工基本信息
     * @param empId
     * @return
     */
    @GetMapping("/query/{empId}")
    public R<Employee> query(@PathVariable Long empId){
        Employee employee = employeeService.getById(empId);
        if(employee.getType() ==2 ){
            Calendar calendar = Calendar.getInstance();
            int age = calendar.get(Calendar.YEAR) - employee.getBirthDate().getYear() - 1;
            if((calendar.get(Calendar.MONTH)+1) - employee.getBirthDate().getMonthValue() > 0 && calendar.get(Calendar.DAY_OF_MONTH) - employee.getBirthDate().getDayOfMonth() > 0
            ){
                age++;
            }
            employee.setAge(age);
            employeeService.updateById(employee);
        }
        return R.success(employee);
    }

    //模糊查询
    @GetMapping("/queryList")
    public R<List<Employee>> queryList(String name,Integer type){
        return employeeService.seleteList(name,type);
    }

    //修改密码
    @PostMapping("/changePassword")
    public R changePassword(@RequestBody Employee employee){
        return employeeService.modifyPassword(employee);
    }

    //手机号登录
    @PostMapping("/loginPhone")
    public R phoneLogin(@RequestBody Employee employee){
       return employeeService.phoneLogin(employee);
    }

    //获取用户各个角色的占比
    @GetMapping("/ratio")
    public R getEmpTypeRatio(){
        return employeeService.getRatio();
    }

    //获取首页数据
    @GetMapping("/homeInfo")
    public R getHomeInfo(){
        return employeeService.getHomeInfo();
    }
}
